package com.ibm.wd150.utilities;

import com.ibm.wd150.passengers.Passenger;
import com.ibm.wd150.passengers.PassengerName;
import com.ibm.wd150.ticketing.Seat;
import com.ibm.wd150.ticketing.Ticket;


public class TicketIssuer {
	
	private Ticket ticket;

	private Passenger getPassenger() {
		String firstName = new UserPrompter("First name?").getAnswer();
		String lastName = new UserPrompter("Last name?").getAnswer();
		String initial = new UserPrompter("Initial?").getAnswer();
		PassengerName pName = new PassengerName(firstName, initial, lastName);
		return new Passenger(pName);
	}

	private Seat assignSeat() {
		Seat seat = new Seat();
		int row = 0;
		while (row < 1 ) {
			row = Integer.parseInt( new UserPrompter("Select row (1-37)").getAnswer());
			if ( row < 1 || row > 37) row =-1;
		}
		seat.setRow(row);
		char letter = 'Z';
		UserPrompter up = new UserPrompter("Select seat in range ABCDEFGH" );
		while (letter == 'Z') {
			letter = up.getAnswer().charAt(0);
			if ( letter < 'A' || letter > 'H') letter = 'Z';
		}
		seat.setLetter(letter);
		return seat;
	}



	public boolean issueTicket() {
		UserPrompter prompter = new UserPrompter(
				"Do you want to purchace a ticket?");
		if (!prompter.getYesNoAnswer()) {
			return false;
		}		
		Passenger passenger = getPassenger();
		Seat seat = assignSeat();
		ticket = new Ticket(passenger, seat, 500.0);
		seat.setTicket(ticket);
		System.out.println("Ticket Issued:");
		return true;
	}
	
	public static void main(String[] args ) {
		TicketIssuer ti = new TicketIssuer();
		if ( ti.issueTicket() ) {
			System.out.println("       " + ti.getTicket());
		} else {
			System.out.println("No ticket issued.");
		}
	}

	public Ticket getTicket() {
		return ticket;
	}

	public void setTicket(Ticket ticket) {
		this.ticket = ticket;
	}
	
}
